package com.lqs.sofp.dao.impl;

import com.lqs.sofp.dao.UserInfoDao;
import com.lqs.sofp.entity.User;
import com.lqs.sofp.utils.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
 * @author ljy
 */
public class UserInfoDaoImpl implements UserInfoDao {
    @Override
    public boolean update(User user) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            String sql = "INSERT INTO UserInfo(Username,Password,Phone,Gender) VALUES(?,?,?,?);";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, user.getUsername());
            stmt.setString(2, user.getPassword());
            stmt.setString(3, user.getPhone());
            stmt.setString(4, user.getGender());

            int num = stmt.executeUpdate();
            if (num > 0) {
                System.out.println("——成功添加——");
                return true;
            } else {
                System.out.println("——添加失败——");
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("——添加失败——");
            return false;
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
    }

    @Override
    public boolean delete(String userName) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            String sql = "delete from UserInfo where Username=?";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, userName);
            int num = stmt.executeUpdate();
            if (num > 0) {
                System.out.println("——删除成功——");
                return true;
            } else {
                System.out.println("——删除失败——");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return false;
    }

    @Override
    public User select(String userName) {
        User user = null;
        //建立连接
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DbUtils.getConnection();
            String sql = "SELECT *FROM UserInfo where Username = ?";
            //String sql = "SELECT *FROM userinfo where Username = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, userName);
            rs = stmt.executeQuery();

            //用来获取应该返回的结果
            rs.last();
            int count = rs.getRow();
            rs.beforeFirst();

            //业务逻辑判断
            if (count != 0) {
                //说明找得到具体用户信息
                while (rs.next()) {
                    String username = rs.getString("Username");
                    String password = rs.getString("Password");
                    String phone = rs.getString("Phone");
                    String gender = rs.getString("Gender");
                    //System.out.println(Username + "\t" + Password + "\t" + Phone + "\t" + Gender);//用于测试输出
                    User temp = new User(username, password, phone, gender);
                    user = temp;
                    //System.out.println(user.getUsername() + "\t" + user.getPassword() + "\t" + user.getPhone() + "\t" + user.getPhone());//用于测试输出
                }
            } else {
                System.out.println("——数据库中找不到对应的用户信息！——");
                //说明找不到用户信息。
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtils.release(rs, stmt, conn);
        }
        return user;
    }

    @Override
    public ArrayList<User> selectAll() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        ArrayList<User> userList = new ArrayList<>();
        try {
            String sql = "select *from UserInfo";
            // 连接
            conn = DbUtils.getConnection();
            // 获取对象
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                String username = rs.getString("Username");
                String password = rs.getString("Password");
                String phone = rs.getString("Phone");
                String gender = rs.getString("Gender");
                User user = new User(username, password, phone, gender);
                userList.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }
}
